<?php

namespace services\models\minisite;

use Yii;

/**
 * This is the model class for table "d_mini.t_activity_show".
 *
 * @property integer $id
 * @property string $c_name
 * @property integer $c_image
 * @property integer $c_sort
 * @property string $c_begin
 * @property string $c_end
 * @property integer $c_status
 * @property string $c_content
 * @property string $db_time
 * @property integer $op_id
 * @property integer $c_date
 */
class MActivityShow extends \common\models\minisite\MActivityShow
{

    //保存之前处理数据
    public function beforeSave($insert)
    {


        $this->c_begin = empty($this->c_begin) ? '1970-01-01 00:00:00' : $this->c_begin;
        $this->c_end = empty($this->c_end) ? '1970-01-01 00:00:00' : $this->c_end;


        if ($insert) {
            $this->db_time = date("Y-m-d H:i:s");
            $this->op_id = Yii::$app->user->identity->no;
        }

        return parent::beforeSave($insert);;
    }

    //更新之前填充值
    public function afterFind()
    {
        parent::afterFind();
        $this->c_begin = $this->c_begin == '1970-01-01 00:00:00' ? '' : $this->c_begin;
        $this->c_end = $this->c_end == '1970-01-01 00:00:00' ? '' : $this->c_end;

    }

    //注释
    public function getMActivityShowFells()
    {
        return $this->hasOne(MActivityShowFeel::className(), ['as_id' => 'id']);
    }

    /**
     *获取活动Banner
     */
    public static function getActivityInfos()
    {
        $query = self::find()
            ->andFilterWhere(['<', 'c_begin', date('Y-m-d H:i:s')])->orFilterWhere(['c_begin' => '1970-01-01 00:00:00'])
            ->andFilterWhere(['>', 'c_end', date('Y-m-d H:i:s')])->orFilterWhere(['c_end' => '1970-01-01 00:00:00'])
            ->andFilterWhere(['c_status' => '1'])
            ->orderBy(['c_sort' => SORT_ASC, 'db_time' => SORT_DESC])
            ->limit(5)
            ->select(['c_image', 'id', 'c_begin', 'c_end', 'c_status']);
        return $query->all();
    }


}
